package com.sirun.cn.fca.module.main;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.githang.statusbar.StatusBarCompat;
import com.sirun.cn.fca.R;
import com.sirun.cn.fca.widget.NoScrollViewPager;
import com.tencent.cloud.uikit.ui.BaseIovActivity;

public class MainActivity extends BaseIovActivity {
    private static final String TAG = MainActivity.class.getSimpleName();

    private static final int OFF_SCREEN_PAGE_LIMIT = 1;

    private NoScrollViewPager mViewPager;
    private RadioGroup mTabGroup;
    private int mCurPos = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        initView();
        initTabGroup();
    }

    private void initView() {
        mViewPager = findViewById(R.id.main_view_pager);
        mViewPager.setNoScroll(true);
        mViewPager.setOffscreenPageLimit(OFF_SCREEN_PAGE_LIMIT);
        mViewPager.setAdapter(new MainPagerAdapter(getSupportFragmentManager()));
        mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                if (position != mCurPos) {
                    RadioButton tab = (RadioButton) mTabGroup.getChildAt(mCurPos);
                    tab.setChecked(false);

                    tab = (RadioButton) mTabGroup.getChildAt(position);
                    tab.setChecked(true);
                    mCurPos = position;
                }
            }
        });
    }

    private void initTabGroup() {
        mTabGroup = findViewById(R.id.tab_radio_group);

        mTabGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.main_radiobutton_home:
                        mViewPager.setCurrentItem(0, false);
                        break;
                    case R.id.main_radiobutton_car:
                        mViewPager.setCurrentItem(1, false);
                        break;
                    case R.id.main_radiobutton_map:
                        mViewPager.setCurrentItem(2, false);
                        break;
                    case R.id.main_radiobutton_me:
                        mViewPager.setCurrentItem(4, false);
                        break;
                    default:
                        break;
                }
            }
        });
    }

}
